package com.teufandsub.socialproximity.logic;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.teufandsub.socialproximity.controller.Constants;
import com.teufandsub.socialproximity.services.HttpService;

public class RegistrationInfo {
	
	public static final String FIRSTNAME_NAME  = "firstname";
	public static final String SURNAME_NAME    = "surname";
	public static final String USERNAME_NAME   = "username";
	public static final String EMAIL_NAME      = "email";
	public static final String PASSWD_NAME     = "passwd";
	public static final String BIRTH_NAME      = "birth";
	public static final String SEX_NAME        = "sex";
	                                           
	public static final int    SEX_UNKNOWN     = 0;
	public static final int    SEX_MALE        = 1;
	public static final int    SEX_FEMALE      = 2;
	
	private String firstname,surname,username,email,passwd;
	private Date birth;
	private int sex;
	
	public RegistrationInfo(String firstname, String surname, String username,
			String email, String passwd, Date birth, int sex) {
		super();
		this.firstname = firstname;
		this.surname = surname;
		this.username = username;
		this.email = email;
		this.passwd = passwd;
		this.birth = birth;
		this.sex = sex;
	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "RegistrationInfo [firstname=" + firstname + ", surname="
				+ surname + ", username=" + username + ", email=" + email
				+ ", passwd=" + passwd + ", birth=" + birth + ", sex=" + sex
				+ "]";
	}

	private JSONObject toJSONObject() throws JSONException {
		JSONObject jSonObject = new JSONObject();
		jSonObject.put(FIRSTNAME_NAME, this.firstname);
		jSonObject.put(SURNAME_NAME, this.surname);
		jSonObject.put(USERNAME_NAME, this.username);
		jSonObject.put(EMAIL_NAME, this.email);
		jSonObject.put(PASSWD_NAME, this.passwd);
		jSonObject.put(BIRTH_NAME, this.birth.getTime());
		jSonObject.put(SEX_NAME, this.sex);
		
		return jSonObject;
	}
	
	public int send(Context context) {
		HttpService httpService = new HttpService(context);
		int ret = -1;
		try {
			ret = httpService.registration(this.toJSONObject());
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e(Constants.APP_NAME, "Error registration ");
		}
		return ret;
	}
}
